package com.lianniu.mgr.buyerManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.buyerManage.dao.IBuyerManageDao;
import com.lianniu.mgr.buyerManage.service.IBuyerConsigneeService;
import com.lianniu.mgr.buyerManage.service.IBuyerManageService;
import com.lianniu.mgr.buyerManage.vo.BuyerConditionVo;
import com.lianniu.mgr.buyerManage.vo.BuyerConsigneeConditionVo;
import com.lianniu.mgr.buyerManage.vo.BuyerConsigneeVo;
import com.lianniu.mgr.buyerManage.vo.BuyerVo;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class BuyerManageServiceImpl implements IBuyerManageService {
  private static Logger logger = LoggerFactory.getLogger(BuyerManageServiceImpl.class);
  @Autowired
  IBuyerManageDao buyerManageDao;
  @Autowired
  IBuyerConsigneeService buyerConsigeeService;
  
  @Override
  public BaseResult getBuyerListForPage(BuyerConditionVo condition, PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    List<BuyerVo> buyerList = buyerManageDao.getBuyerListForPage(condition);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", buyerList);

    return BaseResult.success(retmap);
  }

  @Override
  public BaseResult getBuyerApproveListForPage(BuyerConditionVo condition, PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    List<BuyerVo> buyerList = buyerManageDao.getBuyerApproveListForPage(condition);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", buyerList);

    return BaseResult.success(retmap);
  }


  @Override
  public BaseResult deleteBuyer(String ids) {
    if(StringUtils.isBlank(ids)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String[] idArr = ids.split(",");
    for(String id : idArr){
      boolean result = buyerManageDao.deleteBuyer(id);
      logger.info(id+"删除结果为："+result);
    }
    
    return BaseResult.success();
  }

  @Override
  public BaseResult checkBuyer(String status, String ids) {
    if(StringUtils.isBlank(ids)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    String[] idArr = ids.split(",");
    for(String id : idArr){
      boolean result = buyerManageDao.checkBuyer(status, id);
      logger.info(id+"审核结果为："+result);
    }
    
    return BaseResult.success();
  }

  @Override
  public BaseResult getBuyerById(String id) {
    BuyerVo buyer = buyerManageDao.getBuyerById(id);
    BuyerConsigneeConditionVo condition = new BuyerConsigneeConditionVo();
    condition.setBuyerId(id);
    @SuppressWarnings("unchecked")
    List<BuyerConsigneeVo> consignees = (List<BuyerConsigneeVo>)buyerConsigeeService.getBuyerConsigneeList(condition).getData();
    buyer.setConsignees(consignees);
    return BaseResult.success(buyer);
  }
  
  public BaseResult updateBuyer(BuyerVo buyer){
    buyerManageDao.updateBuyer(buyer);
    return BaseResult.success();
  }

  public BaseResult modifyBuyer(BuyerVo buyer){
    buyerManageDao.modifyBuyer(buyer);
    return BaseResult.success();
  }

  @Override
  public BaseResult getBuyerByUserId(String userid) {
    BuyerVo buyer = buyerManageDao.getBuyerByUserId(userid);
    return BaseResult.success(buyer);
  }

@Override
public boolean isExistMobile(String mobile) {
    logger.info("BuyerManageServiceImpl isExistMobile begin.");
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(mobile)) {
        return false;
    }
    logger.info("input data check end.");

    logger.info("isExistMobile begin.");
    boolean isExistFlg = buyerManageDao.isExistMobile(mobile);
    boolean retFlg = isExistFlg ? true : false;
    logger.info("isExistMobile end.");

    logger.info("BuyerManageServiceImpl isExistMobile end.");
    return retFlg;
}
}
